package com.kssoft.ebd.controller;

import com.kssoft.ebd.dto.req.*;
import com.kssoft.ebd.dto.rest.IssueLiveStreamDTO;
import com.kssoft.ebd.dto.rest.StartCallDTO;
import com.kssoft.ebd.service.ApiBroadcastService;
import com.kssoft.ebd.service.BroadcastPageService;
import com.kssoft.ebd.spider.common.*;
import com.kssoft.ebd.spider.downloader.HttpClientDownloader;
import com.kssoft.ebd.spider.http.CustomHttpUriRequestConverter;
import com.kssoft.ebd.spider.processor.AjaxProcessor;
import com.kssoft.ebd.spider.test.AnyuePipeline;
import com.kssoft.ebd.spider.test.Anyue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

@RestController
public class BroadcastController {

    @Autowired
    private BroadcastPageService broadcastPageService;

    @Autowired
    private Anyue anyue;
    @Autowired
    private AnyuePipeline anyuePipeline ;

    @Autowired
    private ApiBroadcastService apiBroadcastService;

    @GetMapping("homePage")
    public String homePage() {
        return broadcastPageService.homePage();
    }
    @GetMapping("startBroadcastPage")
    public String startBroadcastPage() {
        return broadcastPageService.startBroadcastPage();
    }
    /**
     * closeBroadcastPage
     */
    @GetMapping("closeBroadcastPage")
    public String closeBroadcastPage() {
        return broadcastPageService.closeBroadcastPage();
    }
    /**
     * 查询区域设备
     */
    @RequestMapping("queryAreaDevice")
    public String queryAreaDevice(AreaDeviceQueryParamDTO areaDeviceQueryParamDTO) {
        return apiBroadcastService.queryAreaDevice(areaDeviceQueryParamDTO);
    }

    /**
     * 查询开播列表
     * @param date
     * @param runningType
     * @param scheduleId
     * @return
     */
    @RequestMapping("queryDayScheduleAction")
    public String queryDayScheduleAction(@RequestParam(required = false) String date,@RequestParam(required = false)  String runningType,@RequestParam(required = false)  String scheduleId) {
        return apiBroadcastService.queryDayScheduleAction(date, runningType, scheduleId);
    }

    /**
     * 查询文件资源列表
     *  ED_STREAM-查询网络直播流列表
     *      * ED_FILE-查询文件资源列表
     * @param audioFileQueryParamDTO
     * @return
     */
    @RequestMapping("queryAudioFile")
    public String queryAudioFile(AudioFileQueryParamDTO audioFileQueryParamDTO) {
        return apiBroadcastService.queryAudioFile(audioFileQueryParamDTO);
    }
    /**
     * 查询网络直播流列表
     */
    @RequestMapping("queryAudioStream")
    public String queryAudioStream(AudioFileQueryParamDTO audioFileQueryParamDTO) {
        return apiBroadcastService.queryAudioStream(audioFileQueryParamDTO);
    }

    /**
     * 开播-直播流
     * @param issueLiveStreamParamDTO
     * @return
     */
    @RequestMapping("issueLiveStream")
    public IssueLiveStreamDTO issueLiveStream(IssueLiveStreamParamDTO issueLiveStreamParamDTO) {
        return apiBroadcastService.issueLiveStream(issueLiveStreamParamDTO);
    }

    /**
     * 关播-直播流
     * @param callRecordId
     * @return
     */
    @RequestMapping("stopIssueAudioFile")
    public String stopIssueAudioFile(String callRecordId) {
        return apiBroadcastService.stopIssueAudioFile(callRecordId);
    }
    /**
     * 关播-直播流---预览
     * @param recordId
     * @return
     */
    @RequestMapping("stopListenCallRecord")
    public String stopListenCallRecord(String recordId) {
        return apiBroadcastService.stopListenCallRecord(recordId);
    }

    /**
     * 语音开播
     * @param startCallParamDTO
     * @return
     */
    @RequestMapping("startCall")
    public StartCallDTO startCall(StartCallParamDTO startCallParamDTO) {
        return apiBroadcastService.startCall(startCallParamDTO);
    }

    /**
     * 语音关播
     * @param callId
     * @return
     */
    @RequestMapping("stopCall")
    public String stopCall(String callId) {
        return apiBroadcastService.stopCall(callId);
    }

    /**
     * 查询关播播放列表状态
     * @param callRecordQueryListParam
     * @return
     */
    @RequestMapping("queryCallRecordAjax")
    public String queryCallRecordAjax(CallRecordQueryListParamDTO callRecordQueryListParam) {
        return apiBroadcastService.queryCallRecordAjax(callRecordQueryListParam);
    }













    @GetMapping("anyue")
    public String anyue() {
        //调用selenium，进行模拟登录
//        anyue.login();
        Spider.create(anyue)
                .setDownloader(new HttpClientDownloader())
                .addUrl("https://192.168.0.223:1443/api/ebd-masterdata/org/getOrgList")
                .addPipeline(anyuePipeline)
                .run();
        ResultItems resultItems = PipelineUtils.webResultItemsHashMap.get(PageModelType.Home);

        return resultItems.get("anyue");
    }

    @GetMapping("ajax")
    public String ajax() throws IOException, ClassNotFoundException, NullCookiesException {
        //调用selenium，进行模拟登录
//        anyue.login();
        AtomicReference<String> result = new AtomicReference<>();
        Map<String, String> homeCookies = CommonUtils.getHomeCookies();


        Site SITE = Site.me().setRetryTimes(3).setSleepTime(100).setTimeOut(3000);


        SITE.addHeader("accessKey", homeCookies.get("accessKey"));

        HttpClientDownloader downloader = new HttpClientDownloader();
        downloader.setHttpUriRequestConverter(new CustomHttpUriRequestConverter());
        Request request = new Request("https://192.168.0.223:1443/api/ebd-masterdata/org/getOrgList");
        request.putExtra("method", "GET");
        request.putExtra("params",  new HashMap<>());
        Spider.create(new AjaxProcessor(page -> {
                    page.putField("anyue", page.getRawText());
                    result.set(page.getRawText());
        }, SITE))
                .setDownloader(downloader)
                .addRequest(request)
                .run();
        return result.get();
    }
}
